﻿
    using Chain.BLL;
//    using ChainStock.Controls;
    using System;
    using System.Data;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Wuqi.Webdiyer;

    public partial class ExpenseHistory : PageBase
    {
        private OrderDetail bllOrderDetail = new OrderDetail();
        private OrderLog bllOrderLog = new OrderLog();


        private void BindExpenseHistory(string strSql)
        {
            int Counts = this.NetPagerParameter.RecordCount;
            strSql = strSql + " and OrderType <> 3 ";
            strSql = strSql + " and OrderLog.OrderShopID = SysShop.ShopID and OrderLog.OrderMemID = Mem.MemID  and OrderLog.OrderUserID = SysUser.UserID";
            strSql = PubFunction.GetShopAuthority(base._UserShopID, "OrderShopID", strSql);
            DataTable dtExpenseHistory = this.bllOrderLog.GetListSP(this.NetPagerParameter.PageSize, this.NetPagerParameter.CurrentPageIndex, out Counts, new string[] { strSql }).Tables[0];
            this.NetPagerParameter.RecordCount = Counts;
            this.NetPagerParameter.CustomInfoHTML = string.Format("<div class=\"results\"><span>当前第{0}/{1}页 共{2}条记录 每页{3}条</span></div>", new object[] { this.NetPagerParameter.CurrentPageIndex, this.NetPagerParameter.PageCount, this.NetPagerParameter.RecordCount, this.NetPagerParameter.PageSize });
            this.rptExpenseHistory.DataSource = dtExpenseHistory;
            this.rptExpenseHistory.DataBind();
            PageBase.BindSerialRepeater(this.rptExpenseHistory, this.NetPagerParameter.PageSize * (this.NetPagerParameter.CurrentPageIndex - 1));
        }

        protected void btnExpenseExcel_Click(object sender, EventArgs e)
        {
            int Counts = this.NetPagerParameter.RecordCount;
            string strSql = this.QueryCondition() + " and OrderType<>3 " + " and OrderLog.OrderShopID = SysShop.ShopID and OrderLog.OrderMemID = Mem.MemID  and OrderLog.OrderUserID = SysUser.UserID";
            strSql = PubFunction.GetShopAuthority(base._UserShopID, "OrderShopID", strSql);
            DataTable dtExpenseHistory = this.bllOrderLog.GetListSP(0xf4240, 1, out Counts, new string[] { strSql }).Tables[0];
            DataExcelInfo.ExpenseHistory(dtExpenseHistory, base._UserName);
        }

        protected void btnRptExpenseQuery_Click(object sender, EventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = 1;
            this.BindExpenseHistory(this.QueryCondition());
        }

        protected void drpPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = 1;
            this.NetPagerParameter.PageSize = int.Parse(this.drpPageSize.SelectedValue);
            this.BindExpenseHistory(this.QueryCondition());
        }

        protected bool GetExpenseIsReturn(string strAccount, int OrderType)
        {
            if (OrderType == 7)
            {
                return false;
            }
            DataTable dt = new OrderLog().GetList(" OldAccount='" + strAccount + "' and OrderType=6 ").Tables[0];
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            return true;
        }

        protected string GetGoodsType(int intGoodsType, float strNumber)
        {
            switch (intGoodsType)
            {
                case 0:
                    return "普通商品";

                case 1:
                    return "服务项目";
            }
            return "";
        }

        protected string GetMemCard(string strMemCard)
        {
            if (strMemCard == "0")
            {
                return "无卡号";
            }
            return strMemCard;
        }

        protected string GetOrderType(int intOrderType)
        {
            string strOrderType = "";
            switch (intOrderType)
            {
                case 0:
                    return "快速消费";

                case 1:
                    return "计时消费";

                case 2:
                    return "商品消费";

                case 3:
                case 5:
                    return strOrderType;

                case 4:
                    return "消费撤单";

                case 6:
                    return "消费退货";

                case 7:
                    return "计次消费";
            }
            return strOrderType;
        }

        public bool GetPrint(int OrderType)
        {
            if ((OrderType >= 3) && (OrderType != 7))
            {
                return false;
            }
            return true;
        }

        protected void NetPagerParameter_PageChanging(object src, PageChangingEventArgs e)
        {
            this.NetPagerParameter.CurrentPageIndex = e.NewPageIndex;
            this.BindExpenseHistory(this.QueryCondition());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                PubFunction.BindMemLevelSelect(this.sltMemLevelID, true);
                this.sltMemLevelID.Items.Insert(1, new ListItem("散 客", "-1"));
                PubFunction.BindShopSelect(base._UserShopID, this.sltShop, true);
                this.txtTotalMoney.Value = "0";
                this.txtDiscountMoney.Value = "0";
                PubFunction.BindShopSelect(base._UserShopID, this.sltConsumptionShop, true);
                this.txtStartTime.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");
                this.txtEndTime.Value = DateTime.Now.ToString("yyyy-MM-dd");
                this.txtUser.Value = base._UserName;
                this.txtShopid.Value = this._UserShopID.ToString();
                PubFunction.Get_PrintTitle(ref this.lblPrintTitle, ref this.lblPrintFoot, base._UserShopID);
                if ((PubFunction.curParameter.dataAuthority != 0) && (base._UserShopID > 1))
                {
                    this.sltConsumptionShop.Value = this._UserShopID.ToString();
                    this.sltConsumptionShop.Attributes.Add("disabled", "disabled");
                }
                this.BindExpenseHistory(this.QueryCondition());
                this.PointNum.Value = PubFunction.GetPointNum("XFJL");
            }
        }

        protected string QueryCondition()
        {
            string strQueryMem = this.txtQueryMem.Value;
            string strMemShopID = this.sltShop.Value;
            string strOrderType = this.sltOrderType.Value;
            string strTotalSymbols = this.sltTotalMoney.Value;
            string strSltLevelID = this.sltMemLevelID.Value;
            string strRemark = PubFunction.RemoveSpace(this.txtRemark.Value);
            string strTotalMoney = (this.txtTotalMoney.Value.Trim() != "") ? this.txtTotalMoney.Value.Trim() : "0";
            decimal dclTotalMoney = decimal.Parse(strTotalMoney);
            string strDiscountSymbols = this.sltDiscountMoney.Value;
            string strDiscountMoney = (this.txtDiscountMoney.Value.Trim() != "") ? this.txtDiscountMoney.Value.Trim() : "0";
            decimal dclDiscountMoney = decimal.Parse(strDiscountMoney);
            string strOrderAccount = PubFunction.RemoveSpace(this.txtOrderAccount.Value);
            string strOrderShopID = this.sltConsumptionShop.Value;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("1=1");
            if (strQueryMem != "")
            {
                strSql.AppendFormat(" and (MemCard = '{0}' or MemName like '%{0}%' or MemMobile = '{0}' or MemCardNumber = '{0}' )", strQueryMem);
            }
            if (strMemShopID != "")
            {
                strSql.AppendFormat(" and MemShopID={0}", int.Parse(strMemShopID));
            }
            if (strOrderType != "")
            {
                if (int.Parse(strOrderType) != -1)
                {
                    strSql.AppendFormat(" and OrderType={0}", int.Parse(strOrderType));
                }
                else
                {
                    strSql.AppendFormat(" and OrderMemID={0}", 0);
                }
            }
            if (dclTotalMoney != 0M)
            {
                strSql.AppendFormat(" and OrderTotalMoney" + strTotalSymbols + "{0}", dclTotalMoney);
            }
            if (dclDiscountMoney != 0M)
            {
                strSql.AppendFormat(" and OrderDiscountMoney" + strDiscountSymbols + "{0}", dclDiscountMoney);
            }
            if (this.txtStartTime.Value != "")
            {
                strSql.AppendFormat(" and OrderCreateTime>='{0}' ", this.txtStartTime.Value);
            }
            if (this.txtEndTime.Value != "")
            {
                string strEndTimes = PubFunction.TimeEndDay(this.txtEndTime.Value);
                strSql.AppendFormat(" and OrderCreateTime<='{0}'", strEndTimes);
            }
            if (strSltLevelID != "")
            {
                strSql.AppendFormat(" and MemLevelID={0}", int.Parse(strSltLevelID));
            }
            if (strRemark != "")
            {
                strSql.AppendFormat(" and OrderRemark like '%{0}%' ", strRemark);
            }
            if (strOrderAccount != "")
            {
                strSql.AppendFormat(" and OrderAccount='{0}'", strOrderAccount);
            }
            if (strOrderShopID != "")
            {
                strSql.AppendFormat(" and OrderShopID={0}", int.Parse(strOrderShopID));
            }
            return strSql.ToString();
        }

        protected void rptExpenseHistory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                DataRowView dr = (DataRowView) e.Item.DataItem;
                if (int.Parse(dr["OrderType"].ToString()) > 1)
                {
                    Repeater rptItem = (Repeater) e.Item.FindControl("rptExpenseDetail");
                    if (rptItem != null)
                    {
                        int recordCount = this.NetPagerParameter.RecordCount;
                        string strSql = " OrderDetail.OrderID=OrderLog.OrderID and OrderDetail.GoodsID=Goods.GoodsID";
                        strSql = strSql + " and OrderDetail.OrderID=" + dr["OrderID"];
                        DataTable dtDetail = this.bllOrderDetail.GetListSP(strSql).Tables[0];
                        rptItem.DataSource = dtDetail;
                        rptItem.DataBind();
                        foreach (RepeaterItem rp in rptItem.Items)
                        {
                            Label lblDetailNum = (Label) rp.FindControl("lblDetailNumber");
                            lblDetailNum.Text = (rp.ItemIndex + 1).ToString();
                        }
                    }
                }
            }
        }
    }


